﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.BUS;
using Restaurant.DTO;

namespace Restaurant.User_Control
{
    public partial class QLNhanVien : UserControl
    {
        public QLNhanVien()
        {
            InitializeComponent();
        }

        NhanVienService nvService = new NhanVienService();
        NhaHangService nhService = new NhaHangService();

        private void LoadLoaiNV()
        {
            DataTable dbtab = nvService.LayTatCaLoaiNV();
            cbLoaiNV.Text = "Tất Cả";
            cbLoaiNV.Items.Add("Tất Cả");
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbdow = dbtab.Rows[i];
                cbLoaiNV.Items.Add(dbdow[1].ToString());
            }
            //cbLoaiNV.Text = "Tất Cả";
        }

        private void LoadNV(DataTable dbtab)
        {
            lv_NV.Items.Clear();
            //DataTable dbtab = nvService.LayTatCaNV();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[3].ToString());
                liv.SubItems.Add(dbrow[4].ToString());
                liv.SubItems.Add(dbrow[5].ToString());
                liv.SubItems.Add(nvService.LayLoaiNV(int.Parse(dbrow[6].ToString())));
                liv.SubItems.Add(nhService.LayTenNH(int.Parse(dbrow[7].ToString())));
                liv.SubItems.Add(dbrow[9].ToString());
                lv_NV.Items.Add(liv);
            }
        }

        private void btnThemNhanVien_Click(object sender, EventArgs e)
        {
            frmThongTinNhanVien frm = new frmThongTinNhanVien();
            frm.ShowDialog();
            DataTable dbtab = nvService.LayTatCaNV();
            LoadNV(dbtab);
        }

        private void btnXoaNhanVien_Click(object sender, EventArgs e)
        {
            try
            {
                int index = lv_NV.FocusedItem.Index;
                if (index >= 0)
                {
                    int ma = int.Parse(lv_NV.Items[index].Text);
                    if (nvService.XoaNhanVien(ma))
                    {
                        DataTable dbtab = nvService.LayTatCaNV();
                        LoadNV(dbtab);
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        private void QLNhanVien_Load(object sender, EventArgs e)
        {
            LoadLoaiNV();
            DataTable dbtab = nvService.LayTatCaNV();
            LoadNV(dbtab);
        }

        private void LoadNVTheoLoai(int maloai)
        {
            DataTable dbtab = nvService.LayNVTheoLoai(maloai);
            LoadNV(dbtab);
        }

        private void cbLoaiNV_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = cbLoaiNV.SelectedIndex;
            string tenloai = cbLoaiNV.Items[index].ToString();
            if (tenloai == "Tất Cả")
            {
                DataTable dbtab = nvService.LayTatCaNV();
                LoadNV(dbtab);
                return;
            }
            int maloai = nvService.LayMaLoaiNV(tenloai);
            LoadNVTheoLoai(maloai);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string ten = textBox1.Text;
            DataTable dbtab = nvService.TimNVTheoTen(ten);
            LoadNV(dbtab);
        }

        private void btnTroVe_Click(object sender, EventArgs e)
        {
            Hide();
        }
        bool fix = false;
        bool lost = false;
        bool dirty = false;

        private void btnSuaNhanVien_Click(object sender, EventArgs e)
        {
            int index = lv_NV.FocusedItem.Index;
            if (index >= 0)
            {
                int ma = int.Parse(lv_NV.Items[index].Text);
                NhanVien nv = nvService.TimNVID(ma);
                frmThongTinNhanVien frm = new frmThongTinNhanVien();
                frm.nv = nv;
                frm.flag = 1;
                frm.fix = fix;
                frm.lost = lost;
                frm.dirty = dirty;
                frm.ShowDialog();
                DataTable dbtab = nvService.LayTatCaNV();
                LoadNV(dbtab);
            }
            else
                return;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            DataTable dbtab = new DataTable();
            if (fix)
                dbtab = nvService.LoadNVDirtyread();
            else
                dbtab = nvService.LoadNVDirtyreadError();
            textBox1.Clear();
            LoadNV(dbtab);

        }

        private void fixloi_CheckedChanged(object sender, EventArgs e)
        {
            if (fixloi.Checked)
                fix = true;
            else
                fix = false;
        }

        private void cblost_CheckedChanged(object sender, EventArgs e)
        {
            if (cblost.Checked)
                lost = true;
            else
                lost = false;
        }

        private void cbdirty_CheckedChanged(object sender, EventArgs e)
        {
            if (cbdirty.Checked)
                dirty = true;
            else
                dirty = false;
        }
    }
}
